package com.example.database_manage.start_load;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.database_manage.R;
import com.example.database_manage.database.CommonDatabase;

import static com.example.database_manage.database.MD5Demo.md5;


/***
 * 用于修改密码
 */

public class password_change extends AppCompatActivity {

    private  Button button_change;
    private  SQLiteDatabase db;
    private  EditText account;
    private  EditText oldpassword ;
    private  EditText newpassword ;
    private  EditText confirm ;
    private  String true_password ="";
    private  TextInputLayout textInputLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_change);
        initView();


        button_change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pc_zhanghao = account.getText().toString();
                String pc_oldmima = md5(oldpassword.getText().toString());
                String pc_newmima = md5(newpassword.getText().toString());
                String pc_con_mima = md5(confirm.getText().toString());
                if (TextUtils.isEmpty(pc_zhanghao) || TextUtils.isEmpty(pc_oldmima) || TextUtils.isEmpty(pc_newmima)
                        || TextUtils.isEmpty(pc_con_mima)) {
                    Toast.makeText(password_change.this, "不可为空！", Toast.LENGTH_SHORT).show();
                } else {
                    //去找真正密码
                    Cursor cursor = db.query("administractor", null, "account = ?", new String[]{account.getText().toString()}, null, null, null);
                    //如果根本没这个账户
                    if (cursor.getCount() == 0) {
                        Toast.makeText(password_change.this, "没有找到该账户", Toast.LENGTH_SHORT).show();
                    } else {
                        while (cursor.moveToNext()) {
                            true_password = cursor.getString(cursor.getColumnIndex("password"));
                        }

                        //如果原密码错误
                        if (!md5(oldpassword.getText().toString()).equals(true_password)) {
                            Toast.makeText(password_change.this, "原密码错误！", Toast.LENGTH_SHORT).show();
                        } else {
                            //如果用户前后输入密码不同
                            if (!newpassword.getText().toString().equals(confirm.getText().toString())) {
                                Toast.makeText(password_change.this, "前后两次输入密码不同！", Toast.LENGTH_SHORT).show();
                            } else {
                                ContentValues values = new ContentValues();
                                values.put("password", md5(newpassword.getText().toString()));
                                //更新数据库
                                db.update("administractor", values, "account = ? ", new String[]{account.getText().toString()});

                                Toast.makeText(password_change.this, "修改成功！", Toast.LENGTH_SHORT).show();

                                startActivity(new Intent(password_change.this, load.class));
                            }
                        }

                    }


                }

            }
        });

        //为新密码进行检查
        newpassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(textInputLayout.getCounterMaxLength()<newpassword.length())
                {
                    textInputLayout.setError("超出字数限制！");
                }
                else
                {
                    textInputLayout.setErrorEnabled(false);
                }

            }
        });

    }
    //绑定组件
    public void initView()
    {
        button_change = findViewById(R.id.pc_ok);

        account = findViewById(R.id.pc_account);

        oldpassword = findViewById(R.id.pc_oldpassword);

        newpassword = findViewById(R.id.pc_newpassword);

        textInputLayout = findViewById(R.id.textinput_newpassword);

        confirm = findViewById(R.id.pc_confirm_newpassword);

        db = new CommonDatabase().getSqliteObject(password_change.this,"test_db");
    }
    //检验正确性
    

}
